//
//  LSPhotoBrowserViewController.swift
//  MicroBlog
//
//  Created by lj on 15/9/14.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

let LSPhotoBrowserCellID = "LSPhotoBrowserCellID"

class LSPhotoBrowserViewController: UIViewController {
    
    //MARK:- 控制器属性
    /// 图片地址数组
    let urls:[NSURL]
    /// 用户选中照片索引
    var selectedIndexPath: NSIndexPath
    
    
    @objc private func closeButtonClieked() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    //TODO: -saveButtonClicked
    @objc private func saveButtonClicked() {
        printLog("saveButtonClicked")
    }
    
    init(urls:[NSURL] ,indexPath:NSIndexPath) {
        self.urls = urls
        selectedIndexPath = indexPath
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    override func loadView() {
        let rect = UIScreen.mainScreen().bounds
        
        view = UIView(frame: rect)
        view.backgroundColor = UIColor.blackColor()
        
        setupUI()
    }

    private func setupUI() {
        
        view.addSubview(collectionView)
        view.addSubview(saveButton)
        view.addSubview(closeButton)
        
        collectionView.frame = view.bounds
        
        saveButton.translatesAutoresizingMaskIntoConstraints = false
        closeButton.translatesAutoresizingMaskIntoConstraints = false
        
        let viewDict = ["saveButton":saveButton,"closeButton":closeButton]
        
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[saveButton(==80)]-(>=8)-[closeButton(==80)]-8-|", options: [], metrics: nil, views: viewDict))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[saveButton]-35-|", options: [], metrics: nil, views: viewDict))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[closeButton]-35-|", options: [], metrics: nil, views: viewDict))
       
        
        prepareForCollectionView()
        
//        closeButton.rac_signalForControlEvents(.TouchUpInside).subscribeNext {[weak self] (btn) -> Void in
//            self?.dismissViewControllerAnimated(true, completion: nil)
//        }
        closeButton.addTarget(self, action: "closeButtonClieked", forControlEvents: .TouchUpInside)
        
        saveButton.addTarget(self, action: "saveButtonClicked", forControlEvents: .TouchUpInside)
    }

    private func prepareForCollectionView() {
    
        collectionView.backgroundColor = UIColor(white: 0.98, alpha: 1.0)
        //注册cell
        collectionView.registerClass(LSPhotoBrowserCell.self, forCellWithReuseIdentifier: LSPhotoBrowserCellID)

        collectionView.dataSource = self
        
        //设置布局属性
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = view.bounds.size
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        collectionView.pagingEnabled = true
    }
    
    
    private lazy var collectionView: UICollectionView = UICollectionView(frame:CGRectZero, collectionViewLayout:UICollectionViewFlowLayout())
    /// 保存按钮
    private lazy var saveButton: UIButton = UIButton(title: "保存", fontSize: 14)
    /// 关闭按钮
    private lazy var closeButton: UIButton = UIButton(title: "关闭", fontSize: 14)
}

// MARK: - UICollectionViewDataSource
extension LSPhotoBrowserViewController:UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return urls.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LSPhotoBrowserCellID, forIndexPath: indexPath) as! LSPhotoBrowserCell
        
        cell.backgroundColor = UIColor.randomColor()
        
        cell.url = urls[indexPath.item]
        
        return cell
        
    }
}
